import {observable, computed, action} from 'mobx'
import {createContext} from 'react'
import ChildrenPageUtils from '@/store/extends'
import {toVideoSurveillance} from '@/utils/pageRedirect'
import {editCamera, selectCameraData} from '@/api/ezviz'
import Taro from '@tarojs/taro'
import deepClone from '@/utils/deepClone'
type PageQuery = {cameraId: string}
class Editvideosurveillance extends ChildrenPageUtils {
  @observable pageQuery: PageQuery = {cameraId: ''}
  @action.bound
  updatePageQuery(params: Partial<Record<string, string>>, update?: boolean) {
    const {cameraId} = params
    if (!cameraId) {
      toVideoSurveillance()
    } else {
      if (!update && cameraId === this.pageQuery.cameraId) {
        console.log('%c updatePageQuery same', 'color:yellow')
        return 'same'
      } else {
        console.log('%c updatePageQuery udpate', 'color:green')
        console.log('当前页面路由携带参数', params)
        this.pageQuery = params as PageQuery
        return 'diff'
      }
    }
  }

  @observable cameraData: Camera.Info = {
    cameraId: -1,
    cameraName: '',
    cameraUrl: '',
    appKey: '',
    appSecret: '',
    accessToken: '',
    userId: -1,
    deviceSerial: '',
    channelNo: ''
  }
  @observable defaultCameraData: Camera.Info = {
    cameraId: -1,
    cameraName: '',
    cameraUrl: '',
    appKey: '',
    appSecret: '',
    accessToken: '',
    userId: -1,
    deviceSerial: '',
    channelNo: ''
  }
  @action.bound
  setCameraDta(data: Camera.Info) {
    this.cameraData = data
    this.defaultCameraData = data
  }
  @action.bound
  clearCameraDta() {
    this.cameraData = {
      cameraId: -1,
      cameraName: '',
      cameraUrl: '',
      appKey: '',
      appSecret: '',
      accessToken: '',
      userId: -1,
      deviceSerial: '',
      channelNo: ''
    }
    this.defaultCameraData = {
      cameraId: -1,
      cameraName: '',
      cameraUrl: '',
      appKey: '',
      appSecret: '',
      accessToken: '',
      userId: -1,
      deviceSerial: '',
      channelNo: ''
    }
  }
  @action.bound
  initCameraDta() {
    let cameraData = deepClone(this.defaultCameraData)
    this.cameraData = cameraData
  }
  @action.bound
  handleInputChange(key: keyof Camera.Info, data: string) {
    if (this.cameraData !== undefined) {
      this.cameraData[key] = data as never
    }
  }

  @action.bound
  updatePageDataSource() {
    selectCameraData({cameraId: this.pageQuery.cameraId})
      .then(res => {
        this.setCameraDta(res.data)
      })
      .catch(err => {
        if (err && err.msg) {
          Taro.showModal({
            content: err.msg
          })
        }
        this.clearCameraDta()
      })
  }
  @action.bound
  validateDataSourceBySubmit(): boolean {
    const {cameraName, cameraUrl, appKey, appSecret, deviceSerial, channelNo} =
      this.cameraData

    if (!cameraName) {
      Taro.showModal({
        content: '摄像头名称未输入'
      })
      return false
    }
    if (!appKey) {
      Taro.showModal({
        content: 'AppKey未输入'
      })
      return false
    }
    if (!appSecret) {
      Taro.showModal({
        content: 'AppSecret未输入'
      })
      return false
    }
    if (!deviceSerial) {
      Taro.showModal({
        content: '设备序列号未输入'
      })
      return false
    }
    if (!channelNo) {
      Taro.showModal({
        content: '通道号未输入'
      })
      return false
    }
    if (!cameraUrl) {
      Taro.showModal({
        content: '监控预览地址未输入'
      })
      return false
    }
    return true
  }
  @action.bound
  handleSubmit() {
    console.log(this.cameraData)
    const {
      cameraName,
      cameraUrl,
      appKey,
      appSecret,
      cameraId,
      deviceSerial,
      channelNo
    } = this.cameraData

    // 校验参数
    if (!this.validateDataSourceBySubmit()) return

    editCamera({
      cameraName,
      cameraUrl,
      appKey,
      appSecret,
      cameraId,
      deviceSerial,
      channelNo
    })
      .then(res => {
        if (res.msg) {
          Taro.showModal({
            content: res.msg
          })
        }
        this.updatePageDataSource()
      })
      .catch(err => {
        if (err) {
          Taro.showModal({
            content: err.msg
          })
        }
      })
  }
}

export const editVideoSurveillanceStore = createContext(
  new Editvideosurveillance()
)
